/*
attachment 
Author: 朱希望
Syntax: $(element).chooseStaff({
			hiddenName:"attachment.attachmentId",
		});
*/
(function($) {
  $.fn.attachment = function(options) {
	  var defaults = {   
	            target      : "attachmentdialog",   			//弹出框DIV的ID名字
	            hiddenName	: "attachment.attachmentId",			//hidden隐藏域的名字，也是后台接受什么参数的名称
	            hiddenId	: "attachmentId",				//hidden隐藏域的ID，如果一个页面使用1次以上本插件，必须要设置不同的hiddenId和hiddenName属性
	            title		: "附件上传"				//弹出框上面显示的title
	        };   
	  
	        var options = $.extend(defaults, options);   
	        //把弹出框的DIV和要传到后台的ID隐藏域加上
	        $('body').append("<div id='"+options.target+"' title='"+options.title+"' style='display:none;'> </div>"); 
	       // $('form').append("<input id='"+options.hiddenId+"' type='hidden' name='"+options.hiddenName+"' value=''");
	       // $('form').append(" <@s.hidden name='"+options.hiddenName+"' id='"+options.hiddenId+"'></@s.hidden>");
	        var target = "#"+options.target;
	       	 $(target).append(
		        	 '<div>'+
		        		'<ul>'+
		        	    '<li id="example">'+ 
		        	    '<button type="button" class="btn" id="upload_button"><span><span>开始上传</span></span></button>&nbsp;&nbsp;'+
		        	    '<p>已上传的文件列表:</p>'+ 
		        	    '<ol class="files"></ol>'+
		        		'</ul>'+ 
		        	 '</div>');
	       	var button = $('#upload_button'), interval;
	        var fileType = "all",fileNum = "one"; 
	        new AjaxUpload(button,{
	            action: 'saveAttachment.action',
	            /*data:{
	                'buttoninfo':button.text()
	            },*/
	            name: 'fileupload',
	            onSubmit : function(file, ext){
	                if(fileType == "pic")
	                {
	                    if (ext && /^(jpg|png|jpeg|gif)$/.test(ext)){
	                        this.setData({
	                            'info': '文件类型为图片'
	                        });
	                    } else {
	                        $('<li></li>').appendTo('#example .files').text('非图片类型文件，请重传');
	                        return false;               
	                    }
	                }
	                            
	                button.text('文件上传中');
	                
	                if(fileNum == 'one')
	                    this.disable();
	                
	                interval = window.setInterval(function(){
	                    var text = button.text();
	                    if (text.length < 14){
	                        button.text(text + '.');                    
	                    } else {
	                        button.text('文件上传中');             
	                    }
	                }, 200);
	            },
	            onComplete: function(file, response){
	                if(response != "success")
	                   // alert(response);
	                    
	                button.text('文件上传');
	                            
	                window.clearInterval(interval);
	                            
	                this.enable();
	                
	                if(response == "success");
	                    $('<li></li>').appendTo('#example .files').text(file);
	                    var idName = response.split(",");
	                   // alert(idName[0]);
	    	       		var temp = "#"+options.hiddenId;
	    	       		//alert(idName[1]);
	    	       		if(idName!=null)
	    	       		{	
	    	       			id = idName[0];
	    	       			//alert(id);
	    	       			//alert("bbb");
	    	       			//$('#uploadMessage').append(idName[1]);
	    	       			$(temp).attr('value',id);
	    	       		}
	            },
	        });
	      //员工出框开始
	    	$(target).dialog({
	    		height :400,
	    		width :500,
	    		resizable :false,
	    		autoOpen :false
	    	});
	    	return  $(this).click(function(){   
	              $(target).dialog('open');
	                return false;   
	            });    
  }
})(jQuery);
